﻿using MvcApplication.Common;
using MvcApplication.Entity;
using MvcApplication.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication.Controllers
{
    public class SiteController : Controller
    {
        public ISiteInfoService _siteInfoService;
        public IDisplayInfoService _displayInfoService;
        public IWebDataService _webDataService;
        public SiteController(ISiteInfoService siteInfoService,
            IDisplayInfoService displayInfoService,
            IWebDataService webDataService)
        {
            _siteInfoService = siteInfoService;
            _displayInfoService = displayInfoService;
            _webDataService = webDataService;
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.访问)]
        public ActionResult Index()
        {
            var siteInfo = SiteInfoSession.SiteInfo;
            return View(siteInfo);
        }

        [HttpPost, Permission(Entity.Menu.网站设置, Entity.PermissionType.编辑), ValidateInput(false)]
        public JsonResult Refresh(SiteInfo siteInfo)
        {
            var info = _siteInfoService.SaveSiteInfo(siteInfo);
            return Json(info);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.访问)]
        public ActionResult Menu()
        {
            var menus = SiteInfoSession.Menus.Menus;
            return View(menus);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.编辑)]
        public JsonResult NewCustomMenu(DisplayMenu menu)
        {
            if (string.IsNullOrEmpty(menu.Name)) throw new NotSupportedException("菜单名称不能为空");

            _siteInfoService.NewCustomMenu(menu);
            return Json(true);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.访问)]
        public ActionResult GetMenus()
        {
            var menus = SiteInfoSession.Menus.Menus;
            return Json(menus, JsonRequestBehavior.AllowGet);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.编辑)]
        public JsonResult SaveMenu(DisplayMenu menu)
        {
            if (string.IsNullOrEmpty(menu.Name)) throw new NotSupportedException("菜单名称不能为空");

            _siteInfoService.SaveMenu(menu);
            return Json(true);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.删除)]
        public JsonResult RemoveMenu(DisplayMenu menu)
        {
            if (menu.ID == 0) throw new NotSupportedException("菜单不能为空");

            _siteInfoService.RemoveMenu(menu);
            return Json(true);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.访问)]
        public ActionResult Slider(DisplayMenu menu)
        {
            return View();
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.访问)]
        public ActionResult GetSlider(Pager page)
        {
            var silders = _siteInfoService.GetSliders(page);
            return Json(silders, JsonRequestBehavior.AllowGet);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.编辑)]
        public JsonResult SaveNewSlider(DisplayInfo slider)
        {
            _displayInfoService.SaveNewSlider(slider);
            return Json(true);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.删除)]
        public JsonResult RemoveSlider(long sliderID)
        {
            _displayInfoService.RemoveSlider(sliderID);
            return Json(true);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.编辑)]
        public ActionResult EditSlider(long slideID)
        {
            var slider = _displayInfoService.GetDisplayInfo(slideID);
            if (slider == null) return HttpNotFound();

            return View(slider);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.编辑)]
        public ActionResult SaveSlider(DisplayInfo slider)
        {
            _displayInfoService.SaveSlider(slider);
            return Json(true);
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.访问)]
        public ActionResult Cache()
        {
            return View();
        }

        [Permission(Entity.Menu.网站设置, Entity.PermissionType.编辑)]
        public JsonResult RemoveCache(Caches cache)
        {
            switch (cache)
            {
                case Caches.分类列表:
                    _webDataService.RemoveGetCategoryCache();
                    break;
                case Caches.文章列表:
                    _webDataService.RemoveGetPostsCache();
                    break;
                case Caches.文章详细:
                    _webDataService.RemoveGetPostCache();
                    break;
                case Caches.信息列表:
                    _webDataService.RemoveGetDisplayInfoCache();
                    break;
                case Caches.全部缓存:
                    _webDataService.RemoveGetCategoryCache();
                    _webDataService.RemoveGetPostsCache();
                    _webDataService.RemoveGetPostCache();
                    _webDataService.RemoveGetDisplayInfoCache();
                    break;
            }
            return Json(true);
        }
    }
}